package org.example.account.service.impl;

import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.example.account.mapper.AccountMapper;
import org.example.account.service.AccountService;
import org.example.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Account findById(Long id) {
        return accountMapper.selectById(id);
    }

    @GlobalTransactional
    @Override
    public void reduceAccountMoney(Long id, Double money) {
        log.info("in global transaction: {}", RootContext.inGlobalTransaction());
        log.info("xid: {}", RootContext.getXID());
        accountMapper.reduceAccountMoney(id, money);
    }
}
